﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using Testicide.Models;

namespace Testicide.DAL
{
    public class TesticideInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<TesticideContext>
    {
        /*The default behavior is to create a database only if it doesn't exist (and throw an exception if the model has changed and the 
         * database already exists). In this section you'll specify that the database should be dropped and re-created whenever the 
         * model changes. Dropping the database causes the loss of all your data. This is generally OK during development, because the 
         * Seed method will run when the database is re-created and will re-create your test data. But in production you generally don't 
         * want to lose all your data every time you need to change the database schema. Later you'll see how to handle model changes by
         * using Code First Migrations to change the database schema instead of dropping and re-creating the database.*/
        protected override void Seed(TesticideContext context)
        {
            var projects = new List<Project>
            {
            new Project{ID=1, Name="Project_1", Url="localhost1234", Date=DateTime.Now},
            new Project{ID=2, Name="Project_2", Url="localhost2345", Date=DateTime.Now},
            new Project{ID=3, Name="Project_3", Url="localhost3456", Date=DateTime.Now},
            };

            projects.ForEach(s => context.Project.Add(s));
            context.SaveChanges();

            var modules = new List<Module>
            {
            new Module{ID=1, Name="Module_1.1", Status="InProgress", Date=DateTime.Now, ProjectID=projects.Single(s=>s.ID == 1).ID},
            new Module{ID=2, Name="Module_1.2", Status="NotStarted", Date=DateTime.Now, ProjectID=projects.Single(s=>s.ID == 1).ID},
            new Module{ID=3, Name="Module_2.1", Status="NotStarted", Date=DateTime.Now, ProjectID=projects.Single(s=>s.ID == 2).ID},
            new Module{ID=4, Name="Module_3.1", Status="NotStarted", Date=DateTime.Now, ProjectID=projects.Single(s=>s.ID == 3).ID},
            };
            modules.ForEach(s => context.Module.Add(s));
            context.SaveChanges();

            var features = new List<Feature>
            {
            new Feature{ID=1, Name="feature_1.1.1", Status="InProgress", Date=DateTime.Now, ModuleID=modules.Single(s=>s.ID == 1).ID},
            new Feature{ID=2, Name="feature_1.2.1", Status="InProgress", Date=DateTime.Now, ModuleID=modules.Single(s=>s.ID == 2).ID},
            new Feature{ID=3, Name="feature_2.1.1", Status="NotStarted", Date=DateTime.Now, ModuleID=modules.Single(s=>s.ID == 3).ID},
            new Feature{ID=4, Name="feature_3.1.1", Status="NotStarted", Date=DateTime.Now, ModuleID=modules.Single(s=>s.ID == 4).ID},
            };
            features.ForEach(s => context.Feature.Add(s));
            context.SaveChanges();

            var scenarioToolBox = new List<ScenarioToolBox>
            {
                new ScenarioToolBox{ID=1, Title="Scenario: New Scenario", Content="Put your scenario here", Language="English"},
                new ScenarioToolBox{ID=2, Title="Scenario: Browsing to a Page", Content="Given I am on 'put the page url here' page \r\nThen I should see 'put the message text here' message", Language="English"},
                new ScenarioToolBox{ID=3, Title="Scenario: Login", Content="Given I am on '...' page \r\nWhen I write '...' in 'username' textfield \r\nAnd I write '...' in 'password' textfield \r\nAnd I press 'login' button \r\nThen I should be on '...' page", Language="English"},
                new ScenarioToolBox{ID=4, Title="Scenario: Create Account", Content="Given I am on '...' page \r\nWhen I write '...' in 'username' textfield \r\nAnd I write '...' in 'password' textfield \r\nAnd I write '...' in 'confirm password' textfield \r\nAnd I write '...' in 'email' textfield \r\nAnd I press 'Create' button \r\nThen I should be on '...' page", Language="English"},
                new ScenarioToolBox{ID=5, Title="Scenario: Click a link", Content="Given I am on 'put the page url here' page \r\nWhen I click on the 'link id' link \r\nThen I should be on 'put the page title here' page", Language="English"},
                new ScenarioToolBox{ID=6, Title="Scenario: Press a Button", Content="Given I am on 'put the page url here' page \r\nWhen I press on the 'button id' button \r\nThen I should be on 'put the page title here' page", Language="English"},
                new ScenarioToolBox{ID=7, Title="Scenario: Fill a TextField then Click a Link", Content="Given I am on 'put the page url here' page \r\nWhen I write '...' in the 'textField id' text field \r\nAnd click on 'link id' link \r\nThen I should be on 'put the page title here' page", Language="English"},
                new ScenarioToolBox{ID=8, Title="Scenario: Fill a TextField then Press a Button", Content="Given I am on 'put the page url here' page \r\nWhen I write '...' in the 'textField id' text field \r\nAnd press on 'button id' button \r\nThen I should be on 'put the page title here' page", Language="English"},
                new ScenarioToolBox{ID=9, Title="سيناريو: الذهاب إلى صفحة", Content="بفرض إنى على الصفحة '...'، \r\nيجب أن أرى الرسالة '...' ", Language="Arabic"}
                
            };
            scenarioToolBox.ForEach(s => context.ScenarioToolBox.Add(s));
            context.SaveChanges();

            
        }
        /*The Seed method takes the database context object as an input parameter, and the code in the method uses
    that object to add new entities to the database. For each entity type, the code creates a collection of new
    entities, adds them to the appropriate DbSet property, and then saves the changes to the database. It isn't
    necessary to call the SaveChanges method after each group of entities, as is done here, but doing that helps
    you locate the source of a problem if an exception occurs while the code is writing to the database.

    To tell Entity Framework to use your initializer class, add an element to the entityFramework element in the application Web.config
    * file (the one in the root project folder), as shown in the following example:
         check web.config, the entityFramework tag*/

    /*The context type specifies the fully qualified context class name and the assembly it's in, and the databaseinitializer type 
     * specifies the fully qualified name of the initializer class and the assembly it's in. (When you don't want EF to use the 
     * initializer, you can set an attribute on the context element: disableDatabaseInitialization="true".)*/
    }
}